package gang.com.twoshopping.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import gang.com.twoshopping.R;
import gang.com.twoshopping.bean.Campaign;
import gang.com.twoshopping.bean.HomeCampaign;
import gang.com.twoshopping.bean.HomeCategory;

/**
 * Created by xiaogangzai on 2017/5/21.
 */

public class HomeCatgoryAdapter extends RecyclerView.Adapter<HomeCatgoryAdapter.ViewHolder> {
    private static final int VIEW_TYPE_LEFT = 0;
    private static final int VIEW_TYPE_RIGHT = 1;

    private List<HomeCampaign> mData;
    private OnItemClickListener listener;
    private Context context;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public HomeCatgoryAdapter(List<HomeCampaign> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    //根据不同的函数返回不同的布局
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        //区分左边和右边，创建viewholder
        if (viewType == VIEW_TYPE_RIGHT) {
            return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview_r, parent, false));
        }
        return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview_l, parent, false));
    }

    //数据和控件进行绑定
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeCampaign homeCategory = mData.get(position);
        holder.textTitle.setText(homeCategory.getTitle());
        //  holder.imageViewBig.setImageResource(homeCategory.getCpOne().getImgUrl());
        Picasso.with(context).load(homeCategory.getCpOne().getImgUrl()).into(holder.imageViewBig);
        Picasso.with(context).load(homeCategory.getCpTwo().getImgUrl()).into(holder.imageViewSmallTop);
        Picasso.with(context).load(homeCategory.getCpThree().getImgUrl()).into(holder.imageViewSmallBottom);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    //item下标的一个判断
    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return VIEW_TYPE_RIGHT;
        }
        return VIEW_TYPE_LEFT;
    }

    //static 静态修饰去掉才可以获取外面的
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textTitle;
        ImageView imageViewBig;
        ImageView imageViewSmallTop;
        ImageView imageViewSmallBottom;

        public ViewHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.text_title);
            imageViewBig = (ImageView) itemView.findViewById(R.id.imgview_big);
            imageViewSmallTop = (ImageView) itemView.findViewById(R.id.imgview_small_top);
            imageViewSmallBottom = (ImageView) itemView.findViewById(R.id.imgview_small_bottom);
            imageViewBig.setOnClickListener(this);
            imageViewSmallTop.setOnClickListener(this);
            imageViewSmallBottom.setOnClickListener(this);
            /* //整体的添加点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v, getLayoutPosition());
                    }
                }
            });*/
        }

        @Override
        public void onClick(View v) {
            HomeCampaign homeCampaign = mData.get(getLayoutPosition());
            if (listener != null) {
                switch (v.getId()) {
                    case R.id.imgview_big:
                        listener.onClick(v, homeCampaign.getCpOne());
                        break;
                    case R.id.imgview_small_top:
                        listener.onClick(v, homeCampaign.getCpTwo());
                        break;
                    case R.id.imgview_small_bottom:
                        listener.onClick(v, homeCampaign.getCpThree());
                        break;
                }
            }
        }
    }

    //接口一定要写public，不写默认是provite
    public interface OnItemClickListener {
        void onClick(View v, Campaign campaign);
    }
}
